###############################################
# Meta-analysis 19: Institutional mistrust
###############################################

# 0) User paths
PATH <- ""   

path_meta_instmistrust        <- file.path(PATH, "Datasets", "meta.instmistrust.csv")
path_meta_instmistrust_biv    <- file.path(PATH, "Datasets", "meta.instmistrust_biv.csv")
path_meta_instmistrust_random <- file.path(PATH, "Datasets", "meta.instmistrust_random.csv")
path_out_csv                  <- file.path(PATH, "Results",  "19.results_instmistrust_models.csv")

# 1) Settings
outcome_name <- "Institutional mistrust"

# 2) Packages
suppressPackageStartupMessages({ library(metafor) })

# 3) Load data
meta.instmistrust        <- read.csv(path_meta_instmistrust,        stringsAsFactors = FALSE)
meta.instmistrust_biv    <- read.csv(path_meta_instmistrust_biv,    stringsAsFactors = FALSE)
meta.instmistrust_random <- read.csv(path_meta_instmistrust_random, stringsAsFactors = FALSE)

# 4) Basic checks
required_cols <- c("authoryear", "yi", "vi")
stopifnot(all(required_cols %in% names(meta.instmistrust)))
stopifnot(all(required_cols %in% names(meta.instmistrust_biv)))
stopifnot(all(required_cols %in% names(meta.instmistrust_random)))

# 5) Full, all 3-level RE
full_all_fit <- rma.mv(yi, vi, random = ~ 1 | authoryear/row.names(meta.instmistrust),
                       data = meta.instmistrust, test = "t")
full_all_df <- data.frame(
  coef = unname(full_all_fit$beta), ci.lb95 = full_all_fit$ci.lb, ci.ub95 = full_all_fit$ci.ub,
  pval = full_all_fit$pval, outcome = outcome_name,
  n           = length(unique(meta.instmistrust$authoryear)), 
  k           = nrow(meta.instmistrust),
  sample_type = "All", model_type = "Full"
)

# 6) Bivariate, all 3-level RE
biv_all_fit <- rma.mv(yi, vi, random = ~ 1 | authoryear/row.names(meta.instmistrust_biv),
                      data = meta.instmistrust_biv, test = "t")
biv_all_df <- data.frame(
  coef = unname(biv_all_fit$beta), ci.lb95 = biv_all_fit$ci.lb, ci.ub95 = biv_all_fit$ci.ub,
  pval = biv_all_fit$pval, outcome = outcome_name,
  n           = length(unique(meta.instmistrust_biv$authoryear)), 
  k           = nrow(meta.instmistrust_biv),
  sample_type = "All", model_type = "Bivariate"
)

# 7) Full, quasi-experimental 3-level RE
full_random_fit <- rma.mv(yi, vi, random = ~ 1 | authoryear/row.names(meta.instmistrust_random),
                          data = meta.instmistrust_random, test = "t")
full_random_df <- data.frame(
  coef = unname(full_random_fit$beta), ci.lb95 = full_random_fit$ci.lb, ci.ub95 = full_random_fit$ci.ub,
  pval = full_random_fit$pval, outcome = outcome_name,
  n           = length(unique(meta.instmistrust_random$authoryear)), 
  k           = nrow(meta.instmistrust_random),
  sample_type = "Quasi-experimental", model_type = "Full"
)

# 8) Combine and save
out_models <- rbind(full_all_df, biv_all_df, full_random_df)
print(out_models, row.names = FALSE)
if (!is.null(path_out_csv)) write.csv(out_models, path_out_csv, row.names = FALSE)
